﻿using System;
using System.Data;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

using dotnetCHARTING;
using Rmlife.Config;

namespace Rmlife.Site
{
    public class MyChart
    {

        #region 属性
        private string _phaysicalimagepath = WebConfigs.Paths.UploadRoot + "Chart";//图片存放路径
        private string _title; //图片标题
        private string _xtitle;//图片x座标名称
        private string _ytitle;//图片y座标名称
        private string _seriesname;//图例名称
        private int _picwidth;//图片宽度
        private int _pichight;//图片高度
        private DataTable _dt;//图片数据源

        /// <summary>
        /// 图片存放路径
        /// </summary>
        public string PhaysicalImagePath
        {
            set { _phaysicalimagepath = value; }
            get { return _phaysicalimagepath; }
        }
        /// <summary>
        /// 图片标题
        /// </summary>
        public string Title
        {
            set { _title = value; }
            get { return _title; }
        }
        /// <summary>
        /// 图片标题
        /// </summary>
        public string XTitle
        {
            set { _xtitle = value; }
            get { return _xtitle; }
        }
        /// <summary>
        /// 图片标题
        /// </summary>
        public string YTitle
        {
            set { _ytitle = value; }
            get { return _ytitle; }
        }

        /// <summary>
        /// 图例名称
        /// </summary>
        public string SeriesName
        {
            set { _seriesname = value; }
            get { return _seriesname; }
        }
        /// <summary>
        /// 图片宽度
        /// </summary>
        public int PicWidth
        {
            set { _picwidth = value; }
            get { return _picwidth; }
        }
        /// <summary>
        /// 图片高度
        /// </summary>
        public int PicHight
        {
            set { _pichight = value; }
            get { return _pichight; }
        }
        /// <summary>
        /// 图片数据源
        /// </summary>
        public DataTable DataSource
        {
            set { _dt = value; }
            get { return _dt; }
        }
        #endregion

        #region 构造函数
        public MyChart()
        {
        }

        public MyChart(string PhaysicalImagePath, string Title, string XTitle, string YTitle, string SeriesName)
        {
            _phaysicalimagepath = PhaysicalImagePath;
            _title = Title;
            _xtitle = XTitle;
            _ytitle = YTitle;
            _seriesname = SeriesName;
        }
        #endregion

        #region 输出柱形图
        /// <summary>
        /// 柱形图
        /// </summary>
        /// <returns></returns>
        public void CreateColumn(dotnetCHARTING.Chart chart, SeriesCollection sc)
        {
            chart.Title = this._title;
            chart.XAxis.Label.Text = this._xtitle;
            chart.YAxis.Label.Text = this._ytitle;
            chart.TempDirectory = this._phaysicalimagepath;
            chart.LegendBox.Background.Color = Color.LightGray;
            chart.Type = ChartType.Combo;
            chart.DefaultSeries.DefaultElement.ShowValue = true;
            chart.DefaultSeries.DefaultElement.SmartLabel.Alignment = LabelAlignment.OutsideTop;
            chart.DefaultSeries.DefaultElement.LabelTemplate = "%value(%yPercentOfTotal)";
            chart.ShadingEffectMode = ShadingEffectMode.Two;
            chart.XAxis.Label.Alignment = StringAlignment.Near;

            chart.DefaultSeries.DefaultElement.Marker.Size = 10;
            chart.DefaultSeries.DefaultElement.Transparency = 20;
            chart.DefaultSeries.Line.Width = 9;
            chart.DefaultSeries.DefaultElement.SmartLabel.Line.Width = 3;
            chart.DefaultSeries.DefaultElement.SmartLabel.Line.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            chart.DefaultSeries.DefaultElement.SmartLabel.Line.Color = Color.FromArgb(150, 101, 39, 19);
            chart.DefaultSeries.EmptyElement.Mode = EmptyElementMode.TreatAsZero;


            chart.ShadingEffect = true;
            chart.Use3D = false;

            chart.SeriesCollection.Add(sc);
        }
        #endregion

        #region 输出饼图
        /// <summary>
        /// 饼图
        /// </summary>
        /// <returns></returns>
        public void CreatePie(dotnetCHARTING.Chart chart, SeriesCollection sc)
        {
            chart.Title = this._title;
            chart.TempDirectory = this._phaysicalimagepath;

            chart.TitleBox.Position = TitleBoxPosition.Left;
            //chart.Palette = new Color[] { Color.FromArgb(49, 255, 49), Color.FromArgb(255, 255, 0), Color.FromArgb(255, 99, 49), Color.FromArgb(0, 156, 255) };
            //chart.Size = "500x350";
            chart.LegendBox.Background.Color = Color.LightGray;
            chart.ShadingEffectMode = ShadingEffectMode.One;
            chart.Type = ChartType.Pie;
            chart.ShadingEffect = true;
            chart.Use3D = false;
            chart.PieLabelMode = PieLabelMode.Outside;
            chart.DefaultSeries.DefaultElement.Transparency = 20;
            chart.DefaultSeries.DefaultElement.ShowValue = true;
            chart.DefaultSeries.DefaultElement.LabelTemplate = "%name\n%value(%yPercentOfTotal)";
            chart.DefaultSeries.DefaultElement.SmartLabel.Font = new Font("Arial", 12);
            chart.DefaultSeries.DefaultElement.SmartLabel.PieLabelMode = PieLabelMode.Automatic;
            chart.SeriesCollection.Add(sc);
        }


        private SeriesCollection getArrayData()
        {
            SeriesCollection SC = new SeriesCollection();
            DataTable dt = this._dt;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Series s = new Series();
                s.Name = dt.Rows[i][0].ToString();

                Element e = new Element();

                // 每元素的名称
                e.Name = dt.Rows[i][0].ToString();

                // 每元素的大小数值
                e.YValue = Convert.ToInt32(dt.Rows[i][1].ToString());

                s.Elements.Add(e);
                SC.Add(s);
            }
            return SC;
        }
        #endregion

        #region 输出曲线图
        /// <summary>
        /// 曲线图
        /// </summary>
        /// <returns></returns>
        public void CreateLine(dotnetCHARTING.Chart chart)
        {
            chart.Title = this._title;
            chart.XAxis.Label.Text = this._xtitle;
            chart.YAxis.Label.Text = this._ytitle;
            chart.TempDirectory = this._phaysicalimagepath;
            chart.Type = ChartType.Combo;
            chart.Series.Type = SeriesType.Line;
            chart.Series.Name = this._seriesname;
            chart.Series.Data = this._dt;
            chart.SeriesCollection.Add();
            chart.DefaultSeries.DefaultElement.ShowValue = true;
            chart.ShadingEffect = true;
            chart.Use3D = false;
            chart.Series.DefaultElement.ShowValue = true;
        }
        #endregion

        #region 调用说明及范例
        // MyChart show=new MyChart();
        // show.Title ="2008年各月消费情况统计";
        // show.XTitle ="月份";
        // show.YTitle ="金额(万元)";
        // show.PicHight =300;
        // show.PicWidth =600;
        // show.SeriesName ="具体详情";
        // show.PhaysicalImagePath ="ChartImages";
        // show.DataSource =this.GetDataSource();
        // show.CreateColumn(this.Chart1);
        #endregion

    }
}